package cn.edu.csmzxy.rk233x.auth_system.security.handler;

/**
 * @author wumxing
 * @date 2025/10/15 9:56
 * @description
 */
import com.alibaba.fastjson.JSON;
import cn.edu.csmzxy.rk233x.auth_system.dto.ResultDTO;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {
        // 设置响应格式为JSON，返回401状态码
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        ResultDTO<?> result = ResultDTO.fail(401, "请先登录获取认证Token");
        response.getWriter().write(JSON.toJSONString(result));
    }
}